{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "\n",
    "def create_train_test_split(data_dir, train_file, test_file, train_size=700):\n",
    "    # Ensure reproducibility\n",
    "    random.seed(42)\n",
    "\n",
    "    # Get the list of image files and corresponding mask files\n",
    "    image_dir = os.path.join(data_dir, 'images')\n",
    "    mask_dir = os.path.join(data_dir, 'ground_truth_mask')\n",
    "    \n",
    "    image_files = sorted([f for f in os.listdir(image_dir) if f.endswith('.jpg')])\n",
    "    mask_files = sorted([f for f in os.listdir(mask_dir) if f.endswith('.png')])\n",
    "\n",
    "    # Ensure the number of images and masks are the same\n",
    "    assert len(image_files) == len(mask_files), \"Number of images and masks must match\"\n",
    "\n",
    "    # Create a list of (image, mask) pairs\n",
    "    pairs = list(zip(image_files, mask_files))\n",
    "\n",
    "    # Shuffle the pairs\n",
    "    random.shuffle(pairs)\n",
    "\n",
    "    # Split the pairs into training and testing sets\n",
    "    train_pairs = pairs[:train_size]\n",
    "    test_pairs = pairs[train_size:]\n",
    "\n",
    "    # Write the training pairs to the train_file\n",
    "    with open(train_file, 'w') as tf:\n",
    "        for image, mask in train_pairs:\n",
    "            tf.write(f'images/{image} ground_truth_mask/{mask}\\n')\n",
    "\n",
    "    # Write the testing pairs to the test_file\n",
    "    with open(test_file, 'w') as tf:\n",
    "        for image, mask in test_pairs:\n",
    "            tf.write(f'images/{image} ground_truth_mask/{mask}\\n')\n",
    "\n",
    "# Example usage\n",
    "data_dir = '/root/haiquanLu/data/SOD/data/ECSSD'\n",
    "train_file = 'train_pair.lst'\n",
    "test_file = 'test_pair.lst'\n",
    "create_train_test_split(data_dir, train_file, test_file)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
